package com.music.service.urls.dao.impl;


import static com.music.service.urls.constants.UrlSQLQueries.ADD_NEW_URL_TO_SONG;
import static com.music.service.urls.constants.UrlSQLQueries.GET_URL_FOR_SONGS_FOR_MEMBER;
import static com.music.service.urls.constants.UrlSQLQueries.VIEW_ALL_URLS_OF_SONG;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.RowCallbackHandler;

import com.music.service.urls.beans.Url;
import com.music.service.urls.constants.UrlAccessState;
import com.music.service.urls.constants.UrlState;
import com.music.service.urls.dao.UrlDAO;
import com.music.service.urls.mapper.UrlMapper;

public class UrlDAOImpl implements UrlDAO {
	
	private JdbcTemplate mMusicsTemplate;
	
	
	/**
	 * @return the musicsTemplate
	 */
	public JdbcTemplate getMusicsTemplate() {
		return mMusicsTemplate;
	}

	/**
	 * @param pMusicsTemplate the musicsTemplate to set
	 */
	@Required
	public void setMusicsTemplate(JdbcTemplate pMusicsTemplate) {
		mMusicsTemplate = pMusicsTemplate;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Url> getUrlsOfSong(final Long pSongId){
		List urls = getMusicsTemplate().query(VIEW_ALL_URLS_OF_SONG,
				new PreparedStatementSetter() {
					public void setValues(PreparedStatement preparedStatement)
							throws SQLException {
						preparedStatement.setLong(1, pSongId);
						preparedStatement.setInt(2, UrlAccessState.ACCESSIBLE.getState());
						preparedStatement.setInt(3, UrlState.DELETED.getState());
					}
				}, new UrlMapper());
		return urls;
	}
	
	@Override
	public Map<Long, Url> getUrlsForSongsForMember(final Long pMemberId, Long... pSongIds) {
		final HashMap<Long,Url> songIdUrlMap = new HashMap<Long, Url>();
		//Check alteast one song id is specified.
		if(pSongIds != null && pSongIds.length > 0){
		
			StringBuilder sb = new StringBuilder("");
			for(Long songId : pSongIds){
				sb.append(","+String.valueOf(songId));
			}
			String songIds = sb.substring(1);
			String sql = GET_URL_FOR_SONGS_FOR_MEMBER.replaceFirst("###",songIds);
			
			getMusicsTemplate().query(sql,
				new PreparedStatementSetter() {
					public void setValues(PreparedStatement preparedStatement)
							throws SQLException {
						preparedStatement.setLong(1, pMemberId);
						preparedStatement.setInt(2, UrlState.DELETED.getState());
					}
				},new RowCallbackHandler(){
					public void processRow(ResultSet pResultSet) throws SQLException{
						Url url = new Url();
						url.setUrlId(pResultSet.getLong("url_id"));
						url.setUrlPath(pResultSet.getString("url_path"));
						url.setState(UrlState.getUrlState(pResultSet.getString("state")));
						url.setSongId(pResultSet.getLong("song_id"));
						url.setOwnerId(pResultSet.getLong("created_member_id"));
						url.setAccessState(UrlAccessState.getUrlAccessState(pResultSet.getString("valid_url")));
						url.setCreationDate(pResultSet.getDate("creation_date"));
						songIdUrlMap.put(url.getSongId(),url);
					}
				});
		}
		return songIdUrlMap;
	}

	@Override
	public boolean addNewUrlToSong(final Long pMemberId, final Long pSongId,final String pUrlPath) {
		int nRowsUpdated = getMusicsTemplate().update(ADD_NEW_URL_TO_SONG,
				new PreparedStatementSetter() {
					public void setValues(PreparedStatement preparedStatement)
							throws SQLException {
						preparedStatement.setString(1, pUrlPath);
						preparedStatement.setLong(2, pSongId);
						preparedStatement.setLong(2, pMemberId);
						preparedStatement.setInt(3,UrlState.ACTIVE.getState());
					}
				});
		return nRowsUpdated == 1;
	}

			
	
}
